package ch.drshit.view.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.SpinnerNumberModel;

import ch.drshit.model.picture.Options;

public class MyOptionChooser extends JFrame implements ActionListener{

	private static final long serialVersionUID = 442365473261105284L;

	public interface MyOptionChooserCloseListener{
		public void finish(Options o);
	}
	
	private MyOptionChooserCloseListener l;
	private JButton startButton = new JButton("Start");
	private HashMap<String,JColorChooser> colorChoosers;
	private HashMap<String,JSpinner> percentChoosers;
	private HashMap<String,JSpinner> intChoosers;
	private Options o;
	private Container cP;
	
	public MyOptionChooser(Options options, MyOptionChooserCloseListener l) {
		super("Options");
		this.l = l;
		this.o = options;
		
		colorChoosers = new HashMap<String, JColorChooser>();
		percentChoosers = new HashMap<String, JSpinner>();
		intChoosers = new HashMap<String, JSpinner>();
		cP = this.getContentPane();
		this.setLayout(new BorderLayout());
		JPanel optionPanel = new JPanel();
		optionPanel.setLayout(new GridBagLayout());
		
		JTabbedPane tabedOptions = new JTabbedPane();
		HashMap<String, Byte> percentOptions = options.getPercentOptions();
		for (String percentKey : percentOptions.keySet()) {
			JPanel innerPanel = new JPanel();
			tabedOptions.addTab(percentKey, innerPanel);
			JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
			spinner.setValue(percentOptions.get(percentKey));
			innerPanel.add(spinner);
			percentChoosers.put(percentKey, spinner);
		}
		HashMap<String,Color> colorOptions = options.getColorOptions();
		for (String colorKey : colorOptions.keySet()) {
			JPanel innerPanel =new JPanel();
			tabedOptions.addTab(colorKey, innerPanel);
			JColorChooser colorChooser = new JColorChooser(Color.WHITE);
			colorChoosers.put(colorKey, colorChooser);
			innerPanel.add(colorChooser);
		}
		HashMap<String,Integer> intOptions = options.getIntOptions();
		for (String intKey : intOptions.keySet()) {
			JPanel innerPanel = new JPanel();
			tabedOptions.addTab(intKey, innerPanel);
			JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0.1));
			intChoosers.put(intKey, spinner);
			innerPanel.add(spinner);
		}
		startButton.addActionListener(this);
		this.add(optionPanel, BorderLayout.CENTER);
		optionPanel.add(tabedOptions);
		this.add(startButton, BorderLayout.SOUTH);
		this.setVisible(true);
		this.pack();
	}
	
	private void refreshOptions() {
		
		HashMap<String, Byte> percentOptions = o.getPercentOptions();
		for (String percentKey : percentChoosers.keySet()) {
			percentOptions.put(percentKey, Byte.parseByte(percentChoosers.get(percentKey).getValue().toString()));
		}
		
		HashMap<String, Color> colorOptions = o.getColorOptions();
		for (String colorKey : colorChoosers.keySet()) {
			colorOptions.put(colorKey, colorChoosers.get(colorKey).getColor());
		}
		
		HashMap<String, Integer> intOptions = o.getIntOptions();
		for (String intKey : intChoosers.keySet()) {
			intOptions.put(intKey, (int)(Float.parseFloat((intChoosers.get(intKey).getValue().toString()))));
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource().equals(startButton)) {
			this.refreshOptions();
			l.finish(this.o);
		}
	}
	
	public void closeFrame() {
		this.dispose();
	}
}
